<?php

class Sistema_PaginaController extends App_Controller_Action
{
    private $_table     = 'PAGINA';
    private $_pk        = 'PAG_ID';
    private $_order_col = 'PAG_ORD';
    private $_order     = 'ASC';
    private $_sta_col   = 'PAG_STA';
    private $obj        = '';
    private $form       = '';
    private $_file_col  = 'PAG_FILE'; 
    public  $dir = 'public/upload/pagina/';
    public  $db = '';
    
    public function init()
    {
        if($this->getRequest()->getActionName() == 'edit') $this->form = new Sistema_Form_Pagina();
    }

    public function indexAction() {
        
        Zend_Loader::loadClass('tools','library');
        
        $where = $this->_pk . ' > 0';
        
        $stafiltro = $this->_getParam('stafiltro', '1');
        
        //if($stafiltro != '') $where .= ' AND '.$this->_sta_col.' = '. $stafiltro; 
        
        $this->view->stafiltro = $stafiltro;
        
        $query = 'SELECT SQL_CACHE * FROM '.$this->_table.' WHERE '.$where.' ORDER BY '.$this->_order_col.' '.$this->_order;
        $result = $this->db->fetchAll($query);
        $this->view->result = $this->paginacao($result);
        
    }
    
    public function editAction() {
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_form_pdr.js');
        
        $this->view->headLink()->prependStylesheet($this->_urlbase.'/public/media/js/colorpicker/css/colorpicker.css'); 
        $this->view->headLink()->prependStylesheet($this->_urlbase.'/public/media/js/colorpicker/css/layout.css'); 
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/colorpicker/js/colorpicker.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/colorpicker/js/eye.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/colorpicker/js/utils.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/colorpicker/js/layout.js?ver=1.0.2');
        
        Zend_Loader::loadClass('imagem','library');
        Zend_Loader::loadClass('datetools','library');
        Zend_Loader::loadClass('dominio','library');
        
        $this->view->form = $this->form;
        if($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                if($this->form->isValid($formData)) {
                    $id = (int) $formData[$this->_pk];
                    
                    $returnImg = imagem::upload($this->_file_col,$this->dir,$id);
                    $returnImg2 = imagem::upload($this->_file_col.'2',$this->dir,$id.'_tpp');
                    
                    $data = $formData; 

                    unset($data['img']);
                    unset($data[$this->_file_col]);
                    unset($data['img2']);
                    unset($data[$this->_file_col.'2']);
                    unset($data['Salvar']);
                    unset($data['MAX_FILE_SIZE']);
                    
                    if($id == 0){
                        $this->db->insert($this->_table, $data);
                        $id =  $this->db->lastInsertId();   
                        
                        imagem::rename($id,$this->dir,$returnImg['filenew']);
                        imagem::rename($id.'_tpp',$this->dir,$returnImg2['filenew']);
                    }
                    else $this->db->update($this->_table, $data,$this->_pk.' = ' . $id);
                    
                    if(strlen($_FILES[$this->_file_col]['tmp_name']) > 1){
                        $img = glob($this->dir.$id.'.*');
                        foreach($img as $filetemp) $filename = $filetemp;
                        
                        //imagem::reizeFull($filename,$filename, '378', '235');
                        
                        imagem::delete($id.'_tmn',$this->dir);
                        imagem::delete($id.'_men',$this->dir);
                        imagem::delete($id.'_til',$this->dir);
                        
                        imagem::reizeFull($filename, str_replace('.','_tmn.',$filename), '200', '200');
                        imagem::reizeFull($filename, str_replace('.','_men.',$filename), '66', '66');
                        imagem::reizeFull($filename, str_replace('.','_til.',$filename), '72', '72');
                    }
                    
                    if(strlen($_FILES[$this->_file_col.'2']['tmp_name']) > 1){
                        $img = glob($this->dir.$id.'_tpp.*');
                        foreach($img as $filetemp) $filename = $filetemp;
                        
                        imagem::reizeFull($filename,$filename, '969', '220');

                    }
                    
                    
                    
                    $this->_helper->redirector('index');          
                } else {
                    $this->form->populate($formData);
                }
            } catch(Exception $ex) {
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                $this->form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $data = $this->db->fetchRow('SELECT SQL_CACHE * FROM '.$this->_table.' WHERE '.$this->_pk . ' = ' . $id);
                $this->form->populate($data);
            }
        }
    }

    public function deleteAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = FALSE;
        $id = $this->_getParam('id', 0);
        if ((int) $id > 0 ) {   
            $ok = (bool) $this->db->delete($this->_table, $this->_pk . ' = ' . $id);
            Zend_Loader::loadClass('imagem','library');
            imagem::delete($id,$this->dir,TRUE);
        }      
        echo json_encode(array('ok' => $ok));
    }

}